package com.lecture.java8.excel.dyr.h.a;

import com.alibaba.excel.EasyExcel;
import com.lecture.java8.excel.DemoDataListener;
import com.lecture.java8.excel.dyr.h.Model0725;
import com.lecture.java8.excel.dyr.h.Model0725_2;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Test0725 {

    public static void main(String[] args) {
        String dir = "D:\\study\\tmp\\0725\\01\\";



        String resultPath3 = dir + "上市公司-赫芬达尔指数（2000-2022年.xlsx";
        DemoDataListener<Model0725_2> dataResultListener3 = new DemoDataListener<>();
        EasyExcel.read(resultPath3, Model0725_2.class,dataResultListener3).sheet().doRead();
        //全要素生产率
        List<Model0725_2> resultList3 = dataResultListener3.getDataList();

        Map<String,Model0725_2> result3Map = new HashMap<>();
        for (Model0725_2 model07241 : resultList3) {
            result3Map.put(model07241.getT() + "-" + model07241.getS(),model07241);
        }




        String resultPath = dir + "2013年开始补全后的数据.xlsx";
        DemoDataListener<Model0725_3> dataResultListener = new DemoDataListener<>();
        EasyExcel.read(resultPath, Model0725_3.class,dataResultListener).sheet().doRead();
        //全要素生产率
        List<Model0725_3> resultList = dataResultListener.getDataList();
        for (Model0725_3 model0724 : resultList) {


            Model0725_2 result2 = result3Map.get(model0724.getA() + "-" + model0724.getC());
            if (result2 != null) {
                model0724.setAm(result2.getX());
                model0724.setAn(result2.getY());
            }

        }



        EasyExcel.write( dir + "0725_4结果.xlsx", Model0725_3.class)
                .sheet("a").doWrite(resultList);

    }



    public static void main2(String[] args) {
        String dir = "D:\\study\\tmp\\0725\\";


        String resultPath = dir + "2011年开始的原始数据.xlsx";
        DemoDataListener<Model0725> dataResultListener = new DemoDataListener<>();
        EasyExcel.read(resultPath, Model0725.class,dataResultListener).sheet().doRead();
        //全要素生产率
        List<Model0725> resultList = dataResultListener.getDataList();
        Set<String> codeSet = new HashSet<>();
        for (Model0725 model0724 : resultList) {
            if (model0724.getB().contains("st") || model0724.getB().contains("ST")) {
                codeSet.add(model0724.getA());
            }
        }
        List<Model0725> result1 = new ArrayList<>();
        for (Model0725 model0724 : resultList) {
            if (!codeSet.contains(model0724.getA())) {
                result1.add(model0724);
            }
        }
        result1 = filterCompleteYears(result1,2011,2022);



        EasyExcel.write( dir + "0725结果.xlsx", Model0725.class)
                .sheet("a").doWrite(result1);

    }

    public static List<Model0725> filterCompleteYears(List<Model0725> list, int startYear, int endYear) {
        Set<Integer> requiredYears = IntStream.rangeClosed(startYear, endYear)
                .boxed()
                .collect(Collectors.toSet());

        // Group by code and filter
        Map<String, Set<Integer>> codeToYearsMap = list.stream()
                .collect(Collectors.groupingBy(
                        Model0725::getA,
                        Collectors.mapping(Model0725::getC, Collectors.toSet())
                ));


        Set<String> validCodes = codeToYearsMap.entrySet().stream()
                .filter(entry -> entry.getValue().equals(requiredYears))
                .map(Map.Entry::getKey)
                .collect(Collectors.toSet());

        // Filter original list by valid codes
        return list.stream()
                .filter(obj -> validCodes.contains(obj.getA()))
                .collect(Collectors.toList());
    }
}
